package cn.yzhg.lib.http.tools


import cn.yzhg.lib.tools.LogTools
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

/**
 * 类 名: ResponseLogInterceptor
 * 作 者: yzhg
 * 创 建: 2018/11/16 0016
 * 版 本: 1.0
 * 历 史: (版本) 作者 时间 注释
 * 描 述:
 */
class ResponseLogInterceptor : Interceptor{
    /**
     * 获取网络数据是，找到此信息即可获知，
     */
    private val TAG = "------  OkHttp  ------"
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val response = chain.proceed(request)
        LogTools.instance.i(TAG, "url     =  : " + request.url())
        LogTools.instance.i(TAG, "method  =  : " + request.method())
        LogTools.instance.i(TAG, "headers =  : " + request.headers())
        LogTools.instance.i(TAG, "body    =  : " + request.body()!!)
        LogTools.instance.i(TAG, "code     =  : " + response.code())
        LogTools.instance.i(TAG, "message  =  : " + response.message())
        LogTools.instance.i(TAG, "protocol =  : " + response.protocol())
        if (response.body() != null && response.body()!!.contentType() != null) {
            val mediaType = response.body()!!.contentType()
            val string = response.body()!!.string()
            LogTools.instance.i(TAG, "mediaType =  :  " + mediaType!!.toString())
            LogTools.instance.i(TAG, "string    =  : $string")
            val responseBody = ResponseBody.create(mediaType, string)
            return response.newBuilder().body(responseBody).build()
        } else {
            return response
        }
    }

}